package com.gene.service.impl;

import com.gene.mapper.*;
import com.gene.pojo.*;
import com.gene.pojo.entity.Indent;
import com.gene.pojo.entity.Order;
import com.gene.pojo.entity.Product;
import com.gene.service.ITbOrderService;
import com.gene.utils.UUIDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

@Service
public class TbOrderServiceImpl implements ITbOrderService {
    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Autowired
    private TbOrderItemMapper tbOrderItemMapper;

    @Autowired
    private TbOrderShippingMapper tbOrderShippingMapper;

    @Autowired
    private TbItemMapper mapper;

    @Autowired
    private UpmsAddressMapper upmsAddressMapper;

    /**
     * 新增订单  TbOrder   TbOrderItem   TbOrderShipping
     * @param order
     * @return
     */
    @Transactional
    public boolean add(Order order) {

        TbOrder tbOrder =new TbOrder();
        String orderId=UUIDGenerator.getUUID();
        tbOrder.setOrderId(orderId);
        tbOrder.setPayment(order.getPayment());
        tbOrder.setPaymentType(order.getPaymentType());
        tbOrder.setPostFee(order.getPostFee());
        tbOrder.setStatus(order.getStatus());
        tbOrder.setCloseTime(order.getCloseTime());
        tbOrder.setUpdateTime(order.getUpdateTime());
        tbOrder.setPaymentTime(order.getPaymentTime());
        tbOrder.setConsignTime(order.getConsignTime());
        tbOrder.setEndTime(order.getEndTime());
        tbOrder.setCloseTime(order.getCloseTime());
        tbOrder.setShippingName(order.getShippingName());
        tbOrder.setShippingCode(order.getShippingCode());
        tbOrder.setUserId(order.getUserId());

        TbOrderItem tbOrderItem =new TbOrderItem();
        tbOrderItem.setId(UUIDGenerator.getUUID());
        tbOrderItem.setItemId(order.getItemId().longValue());
        tbOrderItem.setOrderId(orderId);
        tbOrderItem.setNum(order.getNum());
        tbOrderItem.setTitle(order.getTitle());
        tbOrderItem.setPicPath(order.getPicPath());
        tbOrderItem.setTotalFee(order.getTotalFee());
        tbOrderItem.setPrice(order.getPrice());

        TbOrderShipping tbOrderShipping =new TbOrderShipping();
        tbOrderShipping.setOrderId(orderId);
        tbOrderShipping.setReceiverName(order.getReceiverName());
        tbOrderShipping.setReceiverPhone(order.getReceiverPhone());
        tbOrderShipping.setReceiverMobile(order.getReceiverMobile());
        tbOrderShipping.setReceiverState(order.getReceiverState());
        tbOrderShipping.setReceiverCity(order.getReceiverCity());
        tbOrderShipping.setReceiverDistrict(order.getReceiverDistrict());
        tbOrderShipping.setReceiverAddress(order.getReceiverAddress());
        tbOrderShipping.setReceiverZip(order.getReceiverZip());
        tbOrderShipping.setCreated(order.getCreated());
        tbOrderShipping.setUpdated(order.getUpdated());

        int a=tbOrderMapper.insert(tbOrder);
        int b=tbOrderItemMapper.insert(tbOrderItem);
        int c=tbOrderShippingMapper.insert(tbOrderShipping);

        if(a==1 && b==1 && c==1){
            return true;
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//设置手动回滚事务
        return false;
    }


    /**
     * 新增(新)
     * @param indent
     * @return
     */
    @Override
    @Transactional
    public boolean addPage(Indent indent) {
        //根据商品id（itemId）查询商品信息
        TbItem tbItem=new TbItem();
        tbItem.setId(indent.getItemId());
        Product product = mapper.selectId(tbItem);
        if(null==product){
            return false;
        }
        //根据地址id（addressId）查询地址信息
        UpmsAddress upmsAddress = upmsAddressMapper.selectByPrimaryKey(indent.getAddressId());
        if(null == upmsAddress){
            return false;
        }

        //定单实体类
        TbOrder tbOrder =new TbOrder();
        String orderId=UUIDGenerator.getUUID();//生成字符串id
        tbOrder.setOrderId(orderId);
        tbOrder.setUserId(indent.getUserId());
        tbOrder.setPayment(indent.getActualAmount().toString());
        tbOrder.setCreateTime(new Date());
        tbOrder.setStatus(1);//1表示未支付

        TbOrderItem tbOrderItem=new TbOrderItem();
        tbOrderItem.setId(UUIDGenerator.getUUID());
        tbOrderItem.setItemId(indent.getItemId());
        tbOrderItem.setOrderId(orderId);
        tbOrderItem.setNum(indent.getNum());
        tbOrderItem.setTitle(product.getTitle());
        tbOrderItem.setPrice(product.getPrice());
        tbOrderItem.setTotalFee(indent.getTotalFee());
        tbOrderItem.setPicPath(product.getImage());
        tbOrderItem.setActualAmount(indent.getActualAmount());
        tbOrderItem.setDeduct(indent.getDeduct());

        TbOrderShipping tbOrderShipping=new TbOrderShipping();
        tbOrderShipping.setOrderId(orderId);
        tbOrderShipping.setReceiverName(upmsAddress.getRealname());
        tbOrderShipping.setReceiverState(upmsAddress.getReceiverState());
        tbOrderShipping.setReceiverCity(upmsAddress.getReceiverCity());
        tbOrderShipping.setReceiverDistrict(upmsAddress.getReceiverDistrict());
        tbOrderShipping.setCreated(new Date());

        int a=tbOrderMapper.insert(tbOrder);
        int b=tbOrderItemMapper.insert(tbOrderItem);
        int c=tbOrderShippingMapper.insert(tbOrderShipping);

        if(a==1 && b==1 && c==1){
            return true;
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//设置手动回滚事务

        return false;
    }


    /**
     * 查询：  一般查询条件为orderId(订单id)   userId(用户id)
     * @param order
     * @return
     */
    @Override
    public List<Order> query(Order order) {
        String orderId=order.getOrderId();
        Long userId=order.getUserId();

        List<Order> list=tbOrderMapper.query(orderId,userId);

        return list;
    }

    /**
     * 根据userId 、status查询出所有待付款订单 status=(1、待付款；2、待发货；3、待收货；4、已完成)
     * @param order
     * @return
     */
    @Override
    public List<Order> await(Order order) {
        List<Order> list = tbOrderMapper.await(order.getUserId(), order.getStatus());
        return list;
    }

    /**
     * 修改
     * @param order
     * @return
     */
    @Transactional
    public boolean edit(Order order){
        TbOrder tbOrder =new TbOrder();
        tbOrder.setOrderId(order.getOrderId());
        tbOrder.setPayment(order.getPayment());
        tbOrder.setPaymentType(order.getPaymentType());
        tbOrder.setPostFee(order.getPostFee());
        tbOrder.setStatus(order.getStatus());
        tbOrder.setCloseTime(order.getCloseTime());
        tbOrder.setUpdateTime(order.getUpdateTime());
        tbOrder.setPaymentTime(order.getPaymentTime());
        tbOrder.setConsignTime(order.getConsignTime());
        tbOrder.setEndTime(order.getEndTime());
        tbOrder.setCloseTime(order.getCloseTime());
        tbOrder.setShippingName(order.getShippingName());
        tbOrder.setShippingCode(order.getShippingCode());
        tbOrder.setUserId(order.getUserId());

        TbOrderItem tbOrderItem =new TbOrderItem();
        tbOrderItem.setId(order.getId());
        tbOrderItem.setItemId(order.getItemId().longValue());
        tbOrderItem.setOrderId(order.getOrderId());
        tbOrderItem.setNum(order.getNum());
        tbOrderItem.setTitle(order.getTitle());
        tbOrderItem.setPicPath(order.getPicPath());
        tbOrderItem.setTotalFee(order.getTotalFee());
        tbOrderItem.setPrice(order.getPrice());

        TbOrderShipping tbOrderShipping =new TbOrderShipping();
        tbOrderShipping.setOrderId(order.getOrderId());
        tbOrderShipping.setReceiverName(order.getReceiverName());
        tbOrderShipping.setReceiverPhone(order.getReceiverPhone());
        tbOrderShipping.setReceiverMobile(order.getReceiverMobile());
        tbOrderShipping.setReceiverState(order.getReceiverState());
        tbOrderShipping.setReceiverCity(order.getReceiverCity());
        tbOrderShipping.setReceiverDistrict(order.getReceiverDistrict());
        tbOrderShipping.setReceiverAddress(order.getReceiverAddress());
        tbOrderShipping.setReceiverZip(order.getReceiverZip());
        tbOrderShipping.setCreated(order.getCreated());
        tbOrderShipping.setUpdated(order.getUpdated());

        int a=tbOrderMapper.updateByPrimaryKeySelective(tbOrder);
        int b=tbOrderItemMapper.updateByPrimaryKeySelective(tbOrderItem);
        int c=tbOrderShippingMapper.updateByPrimaryKeySelective(tbOrderShipping);

        if(a==1 && b==1 && c==1){
            return true;
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//设置手动回滚事务
        return false;
    }

    /**
     * 删除  直接进行物理删除
     * @param order
     * @return
     */
    @Transactional
    public boolean delete(Order order){
        int a=0;
        int b=0;
        int c=0;
        String orderId =order.getOrderId();
        String id=order.getId();
        if( orderId !=null && orderId.length()>0 && id !=null && id.length()>0){
            a=tbOrderMapper.deleteByPrimaryKey(orderId);
            b=tbOrderItemMapper.deleteByPrimaryKey(id);
            c=tbOrderShippingMapper.deleteByPrimaryKey(orderId);
        }
        if(a==1 && b==1 && c==1){
            return true;
        }
        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//设置手动回滚事务
        return false;
    }
}
